import {inject, observer} from "mobx-react";
import {Modal, Button} from "antd";
import {WeaLocaleProvider, WeaQrcode} from "ecCom";
import * as authTypes from "../../constants/secondAuthType";
import {getCAAuthKey, checkCAAuth, getEMID} from "../../apis/secondAuth";

const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);

        this.state = {sysId: -1};
    }

    componentWillMount() {
        getEMID().then(data => {
            const {success} = data;
            if (success) {
                const {sysId} = data;
                this.setState({sysId});
            }
        });
    }

    componentDidMount() {
        const that = this;
        if (!this.intervalid) {
            this.intervalid = window.setInterval(() => {
                that.authTimter();
            }, 1000);
        }
    }

    componentDidUpdate() {
        const {isShow} = this.props;
        const that = this;
        if (isShow && !this.intervalid) {
            this.intervalid = window.setInterval(() => {
                that.authTimter();
            }, 1000);
        }
    }

    authTimter() {
        const {secondAuthStore, onClose, onSuccess} = this.props;
        const {authKey, secondAuthType, isProtectDialog} = secondAuthStore;
        if (!this.isLoading) {
            this.isLoading = true;
            checkCAAuth({authKey}).then(data => {
                this.isLoading = false;
                const {authStatus, success} = data;
                if (success == 1 && authStatus == 1) {
                    if (!!onSuccess) {
                        onSuccess.call({}, {isProtectDialog});
                    }

                    /*if (!!onClose) {
                        onClose.call({});
                    }*/

                    if (!!this.intervalid) {
                        window.clearInterval(this.intervalid);      //清除定时器
                        this.intervalid = null;
                    }
                }
            });
        }
    }

    componentWillUnmount() {
        if (!!this.intervalid) {
            window.clearInterval(this.intervalid);      //清除定时器
            this.intervalid = null;
        }
    }

    render() {
        const {secondAuthStore} = this.props;
        const {authKey} = secondAuthStore;
        const {sysId} = this.state;
        const url = `/spa/workflow/static4mobile/index.html#/caauth?authKey=${authKey}`;
        const params = {sysId, url};
        return (
            <div className="caWrapper">
                <div className="ca_title wea-f14">{getLabel(501184, "流程已启用CA验证")}</div>
                <div className="description">{getLabel(501189, "请使用e-mobile扫描下方二维码进行验证")}</div>
                <div className="codeWrapper">
                    <div className="qrcode">
                        <WeaQrcode ecId={`${this && this.props && this.props.ecId || ''}_WeaQrcode@tx13f7`} text={`_ec_open_link:${JSON.stringify(params)}`}/>
                    </div>
                </div>
            </div>
        );
    }
}
